<template>
  <div>
    <Header></Header>

    <div class="mblog">
      <h2> {{ blog.title }}</h2>
      <el-link icon="el-icon-edit" v-if="ownBlog">
        <router-link :to="{name: 'BlogEdit', params: {blogId: blog.id}}" >
        编辑
        </router-link>
      </el-link>
      <el-link class="del" icon="el-icon-edit" v-if="ownBlog">
      <div  @click="test()" >
        <h5>删除</h5>
        </div>
      </el-link>
      <el-divider></el-divider>
      <div class="markdown-body" v-html="blog.content"></div>

    </div>

  </div>
</template>

<script>
  import 'github-markdown-css'
  import Header from "../components/Header";

  export default {
    name: "BlogDetail.vue",
    components: {Header},
    data() {
      return {
        blog: {
          id: "",
          title: "",
          content: ""
        },
        ownBlog: false,
      }
    },
    methods: {
      test () {
      const _this = this
      const blogId = this.$route.params.blogId
      this.$axios.get('/blog/delete/' +blogId).then(res => {
       _this.$router.push("/blogs")                 //必须用push 不然不会刷新页面 不能用routerlink
          })
        }
      },
     created() {
      const blogId = this.$route.params.blogId
      console.log(blogId)
      const _this = this
      this.$axios.get('/blog/' + blogId).then(res => {
        const blog = res.data.data
        _this.blog.id = blog.id
        _this.blog.title = blog.title

        var MardownIt = require("markdown-it")
        var md = new MardownIt()

        var result = md.render(blog.content)
        _this.blog.content = result
        _this.ownBlog = (blog.userId == _this.$store.getters.getUser.username)
        console.log(blog.userId,_this.$store.getters.getUser.username)
      })
    }
  }
</script>

<style scoped>
  .mblog {
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    width: 100%;
    min-height: 700px;
    padding: 20px 15px;
  }
  .del {
    float: right;
    margin-right: 50px;
  }
</style>